function custom_wpadmin_password_protect() {
    // Start the session
    if (!session_id()) {
        session_start();
    }

    // Protect both wp-admin and wp-login.php for non-logged-in users
    if ((strpos($_SERVER['REQUEST_URI'], '/wp-admin') !== false || strpos($_SERVER['REQUEST_URI'], '/wp-login.php') !== false) && !is_user_logged_in()) {
        // Set a custom password
        $password = '123456'; // Change this to your desired password

        // Initialize error message
        $error_message = '';

        // Check if the correct password is submitted
        if (isset($_POST['admin_password_idc'])) {
            if ($_POST['admin_password_idc'] === $password) {
                // Set a session variable to allow further access
                $_SESSION['wpadmin_password_idc'] = true;
                
                // Redirect to wp-admin or wp-login.php based on the request
                if (strpos($_SERVER['REQUEST_URI'], '/wp-login.php') !== false) {
                    wp_redirect(site_url('wp-login.php'));
                } else {
                    wp_redirect(admin_url());
                }
                exit;
            } else {
                // Set the error message if the password is incorrect
                $error_message = '<div class="error-message_idc" style="color: red; margin-top: 10px;">Incorrect Password!</div>';
            }
        }

        // If the session is not set or incorrect password, display the custom password form
        if (!isset($_SESSION['wpadmin_password_idc'])) {
            echo '
            <style>
                body {
                    background-color: #21435e;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    height: 100vh;
                    margin: 0;
                }

                .search-container_idc {
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                }

                .search-container_idc input[type="password"] {
                    width: 300px;
                    padding: 10px;
                    border: 2px solid #000000;
                    border-radius: 5px;
                    font-size: 16px;
                }

                .search-container_idc button {
                    background-color: #2271b1; 
                    color: white;
                    padding: 10px 20px;
                    border: none;
                    border-radius: 5px;
                    font-size: 16px;
                    cursor: pointer;
                    margin-top: 10px;
                }

                .search-container_idc button:hover {
                    opacity: 0.6; 
                }

                .error-message_idc {
                    margin-top: 10px;
                    color: red;
                }
            </style>
            <div class="search-container_idc">
                <form method="post">
                    <input type="password" name="admin_password_idc" placeholder="Please Enter Admin Password.." required />
                    <button type="submit">Submit</button>
                </form>
                ' . $error_message . '
            </div>';
            exit;
        }
    }
}
add_action('init', 'custom_wpadmin_password_protect');